package com.liu.controller;

import com.liu.annotation.HasRole;
import com.liu.annotation.Log;
import com.liu.entity.LiuUserRole;
import com.liu.service.LiuUserRoleService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户和角色关联表(LiuUserRole)表控制层
 *
 * @author makejava
 * @since 2023-01-13 20:34:58
 */
@RestController
@RequestMapping("liuUserRole")
public class LiuUserRoleController {
    /**
     * 服务对象
     */
    @Resource
    private LiuUserRoleService liuUserRoleService;

    /**
     * 分页查询
     *
     * @param liuUserRole 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<LiuUserRole>> queryByPage(LiuUserRole liuUserRole, PageRequest pageRequest) {
        return ResponseEntity.ok(this.liuUserRoleService.queryByPage(liuUserRole, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<LiuUserRole> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.liuUserRoleService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param liuUserRole 实体
     * @return 新增结果
     */
    @PostMapping
    @HasRole({"admin","hr"})
    @Log(title = "创建用户",businessType = "用户操作")
    public ResponseEntity<LiuUserRole> add(LiuUserRole liuUserRole) {
        return ResponseEntity.ok(this.liuUserRoleService.insert(liuUserRole));
    }

    /**
     * 编辑数据
     *
     * @param liuUserRole 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<LiuUserRole> edit(LiuUserRole liuUserRole) {
        return ResponseEntity.ok(this.liuUserRoleService.update(liuUserRole));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Long id) {
        return ResponseEntity.ok(this.liuUserRoleService.deleteById(id));
    }

}

